﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Controller;
using Interface;

namespace Projekt3
{
    /// <summary>
    /// Interaction logic for ResultGrid.xaml
    /// </summary>
    public partial class Result : UserControl
    {
        MyController myController;

        int gridSize, pageNum;
        double gWidth, gHeight, margin = 6;

        //session iproduct
        IProduct iProduct;

        public Result(MyController controller, int pageNumber)
        {
            InitializeComponent();

            // MyController
            this.myController = controller;
            this.gridSize = myController.GridSize;
            this.pageNum = pageNumber;

            //initialize content
            Initialize(626, 682);
        }

        public void Initialize(double ucHeight, double ucWidth)
        {
            //(gridWidth - allMarginWidth)/ numOfRows
            //width and height is rounded down
            gWidth = Math.Floor((ucWidth - (margin * (gridSize + 1))) / gridSize);
            gHeight = Math.Floor((ucHeight - (margin * (gridSize + 1))) / gridSize);

            //clear 
            Content.Children.Clear();

            // Produkter
            var products = myController.GetProducts();

            //counter
            int c = pageNum * myController.ProductsPerPage;
            #region row and columns
            for (int i = 0; i < gridSize; i++)
            {
                //column
                for (int j = 0; j < gridSize; j++)
                {
                    if (c < products.Count() && c >= 0)
                    {
                        iProduct = products[c];
                        Grid gridItem = AddGridItem(i, j);
                        Content.Children.Add(gridItem);
                        // Counter
                        c++;
                    }
                }
            }
            #endregion
        }

        private Grid AddGridItem(int row, int column)
        {
            Grid gridItem = new Grid();

            // Grid navn
            gridItem.Name = "g" + iProduct.Id.ToString();

            #region SetGridSizeAndAlignment
            gridItem.Width = gWidth;
            gridItem.Height = gHeight;

            gridItem.HorizontalAlignment = HorizontalAlignment.Left;
            gridItem.VerticalAlignment = VerticalAlignment.Top;
            #endregion

            #region SetMarginThickness
            Thickness marginThickness = new Thickness();
            if (row == 0)
                marginThickness.Top = margin;
            else
                marginThickness.Top = gHeight * row + margin * (row + 1);

            if (column == 0)
                marginThickness.Left = margin;
            else
                marginThickness.Left = gWidth * column + margin * (column + 1);

            gridItem.Margin = marginThickness;
            #endregion

            //height and width for imgplaceholder and textblock
            int labelHeight = 30;
            double controlHeight = (gHeight - (labelHeight + (margin * 4))) / 2;
            double controlWidth = gWidth - (margin * 2);

            #region Add label with product id to grid
            Label lbl = new Label();
            lbl = (Label)AddControl(lbl, iProduct.Id.ToString(), iProduct.ToString(), 30, controlWidth, 0);
            lbl.HorizontalContentAlignment = HorizontalAlignment.Center;
            lbl.Content = iProduct.Id.ToString();
            lbl.Background = Brushes.LightGray;
            gridItem.Children.Add(lbl);
            #endregion

            #region Add placeholder for picture
            Label imgPlaceholder = new Label();

            //startmargin
            double startMargin = labelHeight + margin;
            imgPlaceholder = (Label)AddControl(
                imgPlaceholder,
                "lblImagePlaceholder" + iProduct.Id.ToString(),
                iProduct.Id.ToString(),
                controlHeight,
                controlWidth,
                startMargin
                );
            imgPlaceholder.Background = Brushes.LightSlateGray;
            imgPlaceholder.HorizontalContentAlignment = HorizontalAlignment.Center;
            imgPlaceholder.VerticalContentAlignment = VerticalAlignment.Center;
            imgPlaceholder.FontSize = 20;
            imgPlaceholder.Content = "[" + iProduct.Id + "]";
            gridItem.Children.Add(imgPlaceholder);
            #endregion

            #region Add textblock to show price and launchdate
            Label description = new Label();

            //startmargin
            startMargin = controlHeight + labelHeight + (margin * 2);
            description = (Label)AddControl(
                description,
                "lblDescription" + iProduct.Id.ToString(),
                iProduct.Id.ToString(),
                controlHeight,
                controlWidth,
                startMargin
                );
            description.Content = "Pris: " + iProduct.Price +
                "\r\n" + "Lanceringsdato: " + iProduct.LaunchDate +
                "\r\n" + "Tilgængelighedsniveau: " + iProduct.AvailabilityLevel +
                "\r\n" + "Status: " + iProduct.Status;
            description.Background = Brushes.GhostWhite;
            description.BorderBrush = Brushes.Gray;
            description.BorderThickness = new Thickness(0, 1, 0, 1);
            gridItem.Children.Add(description);
            #endregion

            // Add mousedown event
            gridItem.MouseDown += new MouseButtonEventHandler(Product_MouseDown);

            //cursor
            gridItem.Cursor = Cursors.Hand;

            return gridItem;
        }

        private Control AddControl(Control ctrl, string name, string content, double height, double width, double startPixel)
        {
            ctrl.Height = height;
            ctrl.Width = width;

            ctrl.HorizontalAlignment = HorizontalAlignment.Left;
            ctrl.VerticalAlignment = VerticalAlignment.Top;

            Thickness objMargin = new Thickness();
            objMargin.Top = (2 * margin) + startPixel;
            objMargin.Left = margin;

            ctrl.Margin = objMargin;
            return ctrl;
        }

        private void Product_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Grid g = (Grid)sender;
            int id = int.Parse((g.Name).Remove(0, 1));
            IProduct product = myController.FindProduct(id);
            UC.Product p = new UC.Product(myController, product);
            //http://stackoverflow.com/questions/6682274/change-wpf-windows-label-content-from-child-user-control

            var main = Window.GetWindow(this) as MainWindow;
            if (main != null)
            {
                main.cbGo2Page.Items.Clear();
                main.cbGo2Page.Items.Add(1);
                main.cbGo2Page.SelectedIndex = 0;

                main.btnNext.IsEnabled = false;
            }
            main.gridResult.Children.Clear();
            main.gridResult.Children.Add(p);
            //Content.Children.Clear();
            //Content.Children.Add(p);
        }
    }
}